package com.sjyp.cms.controller.admin;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.sjyp.cms.model.Article;
import com.sjyp.cms.model.Column;

import java.util.Date;
import java.util.List;

public class ArticleController extends BaseController {
    public void index() {
        int pageNumber = this.getParaToInt("pageNumber", 1);
        int pageSize = this.getParaToInt("pageSize", 10);
        String sql = "SELECT " +
                "t_article.id," +
                "t_article.title," +
                "t_article.subtitle," +
                "t_article.summary," +
                "t_article.content," +
                "t_article.columnId," +
                "t_article.orderNo," +
                "t_article.createDate ";
        String sqlExceptSelect = "FROM t_article ";
        String columnId = this.getPara("columnId");
        if (null != columnId && !"0".equals(columnId) && !"null".equals(columnId)) {
            sqlExceptSelect = sqlExceptSelect + "where columnId=" + columnId;
            this.setAttr("currentColumnId", Integer.valueOf(columnId));
        }
        Page<Article> articles = Article.dao.paginate(pageNumber, pageSize, sql, sqlExceptSelect);
        this.setAttr("articles", articles);
        this.setAttr("columns", this.findAllColumn());
        this.render("article_list.html");
    }

    public void save() {
        Article article = this.getBean(Article.class,"");
        article.setCreateDate(new Date());
        boolean isSucess = false;
        if (null == article.getId()) {
            isSucess = article.save();
        } else {
            isSucess = article.update();
        }
        //保存（或修改）后跳转至通用成功（或失败）页面
        this.setCommon("文章","article",isSucess);
    }

    public void goSave() {
        Integer id = this.getParaToInt("id",null);
        if (null != id) {
            Article article = Article.dao.findById(id);
            this.setAttr("article", article);
        }
        List<Column> columns = this.findAllColumn();
        this.setAttr("columns", columns);
        this.render("article_form.html");
    }

    public void del() {
        Integer id = this.getParaToInt("id",null);
        boolean isDelSuccess = Article.dao.deleteById(id);
        this.index();
    }

    public void goTextForm() {
        String articleId = this.getPara("id");
        if (StrKit.isBlank(articleId)) {
            this.index();
            return;
        }
        this.setAttr("id",articleId);
        Article article = Article.dao.findById(Integer.valueOf(articleId));
        this.setAttr("article", article);
        this.render("article_text_form.html");
    }

    public void saveText() {
        Integer id = this.getParaToInt("id",null);
        String text = this.getPara("text");
        boolean isSuccess = new Article().setId(id).setContent(text).update();//this.htmlEncode(text)
        this.setCommon("文章","article",isSuccess);
    }

    /**
     * 获取所有栏目  新增图片 时，下拉框使用
     * @return
     */
    private List<Column> findAllColumn() {
        String sql = "SELECT " +
                "t_column.id," +
                "t_column.`name` " +
                "FROM t_column" ;
        return Column.dao.find(sql);
    }

    private String htmlEncode(String source) {
        if (source == null) {
            return "";
        }
        String html = "";
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < source.length(); i++) {
            char c = source.charAt(i);
            switch (c) {
                case '<':
                    buffer.append("&lt;");
                    break;
                case '>':
                    buffer.append("&gt;");
                    break;
                case '&':
                    buffer.append("&amp;");
                    break;
                case '"':
                    buffer.append("&quot;");
                    break;
                case 10:
                case 13:
                    break;
                default:
                    buffer.append(c);
            }
        }
        html = buffer.toString();
        return html;
    }
}
